package com.onion.utils.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtils {

    private static final String MD5 = "MD5";

    /**
     * 返回MD5 16进制字符串
     * @param content
     * @return
     */
    public static String md5Hex(String content){
        byte[] result = md5Byte(content);
        return ByteUtils.byte2Hex(result);
    }

    /**
     * 返回MD5字符串
     * @param content
     * @return
     */
    public static String md5(String content){
        byte[] result = md5Byte(content);
        return new String(result);
    }

    /**
     *
     * @param content
     * @return
     */
    public static byte[] md5Byte(String content){
        try {
            MessageDigest md = MessageDigest.getInstance(MD5);
            md.update(content.getBytes());
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5出错:" + content);
        }
    }

}